 

package com.zdkj.subway.rca.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zdkj.common.core.util.R;
import com.zdkj.common.log.annotation.SysLog;
import com.zdkj.subway.rca.entity.SwCkCycleStockSale;
import com.zdkj.subway.rca.service.SwCkCycleStockSaleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


/**
 * 数据关联性审核-周期内库存售卖表管理
 *
 * @author stw
 * @date 2020-11-24 11:11:45
 */
@RestController
@AllArgsConstructor
@RequestMapping("/swckcyclestocksale" )
@Api(value = "swckcyclestocksale", tags = "数据关联性审核-周期内库存售卖表管理")
public class SwCkCycleStockSaleController {

    private final SwCkCycleStockSaleService swCkCycleStockSaleService;

    /**
     * 数据关联性审核-周期内库存售卖表管理-生成数据
     * @return R
     */
    @ApiOperation(value = "生成数据", notes = "生成数据")
    @SysLog("数据关联性审核-周期内库存售卖表管理-生成数据" )
    @GetMapping({"/generate"})
    //@PreAuthorize("@pms.hasPermission('sw_ck_cycle_stock_sale_generate')" )
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "startDate", value = "开始日期：yyyy-MM-dd", dataTypeClass = String.class,required = true),
                    @ApiImplicitParam(name = "endDate", value = "结束日期：yyyy-MM-dd", dataTypeClass = String.class,required = true),
            }
    )
    public R generate(String startDate, String endDate) {
        try {
            if (StringUtils.isEmpty(startDate) || StringUtils.isEmpty(endDate)){
                return R.failed("开始日期和结束日期必选");
            }

            boolean flag = swCkCycleStockSaleService.generate(startDate,endDate);

            return R.ok(flag);
        } catch (Exception e) {
            e.printStackTrace();
            return R.failed();
        }

    }

    /**
     * 分页查询
     * @param page 分页对象
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    public R getSwCkCycleStockSalePage(Page page, String startDate, String endDate,String lineCode,String diffCondition,String ticketType ) {
        SwCkCycleStockSale swCkCycleStockSale = new SwCkCycleStockSale();
        swCkCycleStockSale.setLineCode(lineCode);
		if(ticketType!=null && !"".equals(ticketType)){
		   swCkCycleStockSale.setTicketType(ticketType);
		}
        boolean isEqualsZero = StringUtils.isNotEmpty(diffCondition)&&"0".equals(diffCondition);
		boolean isNotEqualsZero = StringUtils.isNotEmpty(diffCondition)&&"1".equals(diffCondition);
		
		/*
		LambdaQueryWrapper<SwCkCycleStockSale> queryWrapper = Wrappers.<SwCkCycleStockSale>query(swCkCycleStockSale).lambda();
		queryWrapper.between(SwCkCycleStockSale::getStatisticalTime, startDate, endDate);
		lambdaQueryWrapper.orderByDesc("CREATE_TIME");
		queryWrapper.orderByAsc("LINE_CODE");
        queryWrapper.orderBy("STATISTICAL_TIME");*/

		//
		QueryWrapper<SwCkCycleStockSale> lambdaQueryWrapper = Wrappers.query(swCkCycleStockSale);
        lambdaQueryWrapper.lambda().ge(StringUtils.isNotEmpty(startDate), SwCkCycleStockSale::getStatisticalTime, startDate).le( StringUtils.isNotEmpty(endDate),SwCkCycleStockSale::getStatisticalTime, endDate).ne(isNotEqualsZero, SwCkCycleStockSale::getDifferenceAmount, 0).eq(isEqualsZero, SwCkCycleStockSale::getDifferenceAmount, 0);
        lambdaQueryWrapper.orderByAsc("LINE_CODE");
		lambdaQueryWrapper.orderByDesc("STATISTICAL_TIME");
       
		//
        //return R.ok(swCkCycleStockSaleService.page(page, Wrappers.query(swCkCycleStockSale).lambda().ge(StringUtils.isNotEmpty(startDate), SwCkCycleStockSale::getStatisticalTime, startDate).le( StringUtils.isNotEmpty(endDate),SwCkCycleStockSale::getStatisticalTime, endDate)));

		return R.ok(swCkCycleStockSaleService.page(page,lambdaQueryWrapper));
    }


    /**
     * 导出
     * @param
     * @return
     * @throws IOException
     */
    @ApiOperation(value = "导出", notes = "导出")
    @GetMapping({"/export"})
    @SysLog("导出(分日期)")
    @PreAuthorize("@pms.hasPermission('sw_ck_cycle_stock_sale_export')")
    public void exportSwCkCycleStockSale(String startDate, String endDate,String lineCode,String ticketType,HttpServletResponse response) {
        swCkCycleStockSaleService.exportSwCkCycleStockSale(startDate,endDate, lineCode,ticketType, response);
    }

}
